﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleGame.Font;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using SimpleGame.ScreenManagement;

namespace SimpleGame.Utils
{
    public class FrameRateCounter : DrawableGameComponent
    {
        #region Fields

        // The content manager to load the font for the fps display
        private ContentManager content;
        // The simple font used to display the fps
        private SimpleFont font;
        // true = fps are shown | false = fps are not shown
        private bool showFPS = false;
        // The current frame rate
        private int frameRate = 0;
        // Counts the frames that are drawn per second
        private int frameCounter = 0;
        // Timer which times until 1 second is reached
        private TimeSpan elapsedTime = TimeSpan.Zero;

        #endregion

        #region Constructor

        public FrameRateCounter(Game game)
            : base(game)
        {
            this.content = game.Content;

            // fps display is disabled by default, though it's enabled if in Debug mode.
            showFPS = false;
#if DEBUG
            showFPS = true;
#endif
        }

        #endregion

        #region XNA Methods

        protected override void LoadContent()
        {
            // load font
            SpriteFont spriteFont = content.Load<SpriteFont>(@"font/fps");
            font = new SimpleFont(spriteFont, "fps");
        }


        protected override void UnloadContent()
        {
        }


        public override void Update(GameTime gameTime)
        {
            // update only if fps are shown
            if (showFPS)
            {
                // timer
                elapsedTime += gameTime.ElapsedGameTime;

                // check if 1 second or more is reached
                if (elapsedTime > TimeSpan.FromSeconds(1))
                {
                    // substract 1 second
                    elapsedTime -= TimeSpan.FromSeconds(1);
                    // pass the frames counted to have the fps available for diplay
                    frameRate = frameCounter;
                    // reset frame counter to begin again with counting
                    frameCounter = 0;
                }
            }

            // F10 toggles fps diplay
            if (InputHandler.IsKeyPressed(Keys.F10))
            {
                showFPS = !showFPS;
            }
        }


        public override void Draw(GameTime gameTime)
        {
            // draw only if fps are shown
            if (showFPS)
            {
                // count frames fully drawn to screen
                frameCounter++;

                // create the string for the fps display in a nice format
                string fps = string.Format("fps: {0}", frameRate);
                // calculate memory usage in MB
                float memInMB = (float)GC.GetTotalMemory(false) / 1024f / 1024f;
                // create the string for the memory display in a nice format
                string mem = string.Format(
                    "memory: {0} MB", memInMB.ToString());

                ScreenManager.SpriteBatch.Begin();

                // draw the fps string
                FontManager.Instance.DrawString(
                    font,
                    fps,
                    new Vector2(20, 20),
                    Color.White,
                    1f,
                    0f,
                    Vector2.Zero);

                // draw the memory string
                FontManager.Instance.DrawString(
                    font,
                    mem,
                    new Vector2(20, 40),
                    Color.White,
                    1f,
                    0f,
                    Vector2.Zero);

                ScreenManager.SpriteBatch.End();

            }
        }

        #endregion
    }
}
